package com.ishow.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ishow.entity.User;
import com.ishow.service.IUserService;

@Controller
public class LoginController {

	@Autowired
	private IUserService userService;

	@RequestMapping("/login")
	public String login(User user,HttpServletRequest request, HttpServletResponse response) {
		User u = userService.selectOne(new EntityWrapper<User>()
				.eq("username", user.getUsername())
				.eq("password", user.getPassword()));
		if (u != null) {
			return "redirect:/index";
		}
		try {
			request.setAttribute("error", "用户名或密码错误");
			request.getRequestDispatcher("index.jsp").forward(request,response);
		} catch (ServletException | IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@RequestMapping("/index")
	public String index() {
		return "index";
	}

}
